package com.apache.funds.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.funds.entity.SvmVoucherDetail;
import com.apache.funds.manager.SvmVoucherDetailManager;

/**
 * 本类主要涉及业务逻辑<br>
 * 目前未启用，其内都是自动生成的
 * @author Administrator
 *
 */
public class SvmVoucherDetailManagerImpl implements SvmVoucherDetailManager {

	protected IDao svmVoucherDetailDao;
	protected final String entityName = "com.apache.funds.entity.SvmVoucherDetail";

	public void setSvmVoucherDetailDao(IDao svmVoucherDetailDao) {
		this.svmVoucherDetailDao = svmVoucherDetailDao;
	}

	public String saveInfo(ParamsVo<SvmVoucherDetail> vo) throws BusinessException {
		SvmVoucherDetail svmVoucherDetail = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="svmVoucherDetail_"+infoId;
		//svmVoucherDetail.setVoucherDetailNum(infoId);
		MethodParam param = new MethodParam("SvmVoucherDetail", cacheKey, "", entityName);
		param.setVaule(svmVoucherDetail);
		if (svmVoucherDetailDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<SvmVoucherDetail> vo) throws BusinessException {
		SvmVoucherDetail svmVoucherDetail = vo.getObj();
		//if (Validator.isNotNull(svmVoucherDetail.getVoucherDetailNum())) {
		String cacheKey = "";
		//String cacheKey="svmVoucherDetail_"+svmVoucherDetail.getVoucherDetailNum();
		MethodParam param = new MethodParam("SvmVoucherDetail", cacheKey, "", entityName);
		param.setVaule(svmVoucherDetail);
		return svmVoucherDetailDao.edit(param);
		//}
		//return false;
	}

	public boolean deleteInfo(ParamsVo<SvmVoucherDetail> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="svmVoucherDetail_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		SvmVoucherDetail info = (SvmVoucherDetail) svmVoucherDetailDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("SvmVoucherDetail");
			return svmVoucherDetailDao.edit(param);
		} else {
			param.setParams("voucherDetailNum", infoId);
			param.setDelete(true);
			return svmVoucherDetailDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<SvmVoucherDetail> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="svmVoucherDetail_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return svmVoucherDetailDao.selectById(param);
	}

	public Object execute(ParamsVo<SvmVoucherDetail> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<SvmVoucherDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = svmVoucherDetailDao.pageSelect(param);
		return page;
	}

	public List<SvmVoucherDetail> getList(ParamsVo<SvmVoucherDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return svmVoucherDetailDao.select(param);
	}

	public long countInfo(ParamsVo<SvmVoucherDetail> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return svmVoucherDetailDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<SvmVoucherDetail> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(svmVoucherDetailDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		SvmVoucherDetail svmVoucherDetail = vo.getObj();
		if (Validator.isNotNull(svmVoucherDetail.getVoucherId())) {
			sb.append(" and voucherId = :voucherId");
			param.setParams("voucherId", svmVoucherDetail.getVoucherId());
		}
		if (Validator.isNotNull(svmVoucherDetail.getCaptionId())) {
			sb.append(" and captionId = :captionId");
			param.setParams("captionId", svmVoucherDetail.getCaptionId());
		}
		if (Validator.isNotNull(svmVoucherDetail.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", svmVoucherDetail.getUserId());
		}
		if (Validator.isNotNull(svmVoucherDetail.getDirection())) {
			sb.append(" and direction = :direction");
			param.setParams("direction", svmVoucherDetail.getDirection());
		}
		if (Validator.isNotNull(svmVoucherDetail.getState())) {
			sb.append(" and state = :state");
			param.setParams("state", svmVoucherDetail.getState());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
